// miniprogram/pages/newProject/output/output.js
var app = getApp();
const host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    outputIndexArray: ['A'],
    outputDict: {
      'A': {
        name: '方案一：',
        src: ""
      },
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //这里是最后一步 向服务器传送所有筛选选项 获得数据
    //再传输给下一个页面
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log("选择完毕: ", app.globalData.newProjectDict);
    var dict = app.globalData.newProjectDict;
    var imageName = ""; //选择的图片名称
    var alphas = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] //一共七个选项
    alphas.forEach(alpha => {
      imageName += alpha;
      imageName += app.globalData.newProjectDict[alpha];
    });
    console.log("图片名称：", imageName)

    wx.request({
      url: host + 'city360/project/getPossibleImageFileNames',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        imageName: imageName
      },
      complete: res => {
        console.log(res);
        if (res.statusCode == 200 && res.data !== "None") {
          console.log("得到的图片路径为：", res.data);
          var imageFileName = res.data;
          that.setData({
            imageFileName: imageFileName
          });
          that.data.outputDict['A'].src = host + imageFileName;
          that.setData({
            outputDict: that.data.outputDict
          });
          wx.hideLoading();
          /*
          wx.downloadFile({
            url: "https://www.city-tech360.com/city360/downloadImg/" + imageFileName,
            success: res=>{
              console.log(res);
              if(res.statusCode === 200){
                var img_path = res.tempFilePath;
                that.data.outputDict['A'].src = img_path;
                that.setData({
                  outputDict: that.data.outputDict
                });
                wx.hideLoading();
              }else{
                wx.showModal({
                  title: '出错了',
                  content: '请稍候再试',
                  showCancel: false,
                });
              }
            }
          })
          */
          //var task = do_download_img(that, imageFileName);
          /*task.then(res=>{
            var img_path = res.tempFilePath;
            setTimeout(() => {
              console.log("保存图片路径为：", img_path);
              that.data.outputDict['A'].src = img_path;
              that.setData({
                outputDict: that.data.outputDict
              });
              wx.hideLoading();
            }, 500);
          });*/
          /*Promise.all([task]).then(results => {
            //下载图片之后 把相关信息添加到outPutDict当中
            console.log(results);
            var res = results[0]
            var img_path = res.tempFilePath;
            setTimeout(() => {
              console.log("保存图片路径为：", img_path);
              that.data.outputDict['A'].src = img_path;
              that.setData({
                outputDict: that.data.outputDict
              });
              wx.hideLoading();
            }, 500);

          });*/
        } else {
          wx.showModal({
            title: '出错了',
            content: '请稍候再试',
            showCancel: false,
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  back: function(e) {
    wx.redirectTo({
      url: '../needCeiling/needCeiling',
    })
  },

  tryAgain: function(e) {
    console.log("试试下一张");
  },

  
  chooseItem: function(e){
    wx.showLoading({
      title: '上传中',
      mask: true,
    });
    var that = this;
    console.log("选取了");
    var type = e.currentTarget.dataset.type;
    console.log(type);
    var app = getApp();
    var imageFileName = that.data.imageFileName
      wx.request({
        url: host + 'city360/project/addScheme',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          projectId: app.globalData.newProjectDict.projectId,
          imageFileName: imageFileName
        },
        complete: res => {
          console.log(res);
          if (res.statusCode.toString()[0] === '2') {
            console.log("add Scheme");
            wx.hideLoading();
            wx.showToast({
              title: '成功',
            });
            setTimeout(()=>{
              wx.navigateBack({
                
              });
            });
          }else{
            wx.hideLoading();
            wx.showModal({
              title: '失败',
              content: '发生错误',
              showCancel: false,
              success: res => {
                wx.navigateBack({
                })
              }
            })
          }
          
        }
      });
  }
    
  
  

})

function do_download_img(page, imageFileName) {
  var downloadTask = wx.downloadFile({
    url: host + "city360/downloadImg/" + imageFileName,
    //filePath: wx.env.USER_DATA_PATH + '/' + imageFileName + '.jpg',
  });
  return downloadTask;
}



//function get_img_path(imageFileName) {
//  return wx.env.USER_DATA_PATH + '/' + imageFileName + '.jpg';
//}